package org.microblog.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.microblog.dao.BlogDao;
import org.microblog.dao.CollectionDao;
import org.microblog.dao.CommentDao;
import org.microblog.dao.LikesDao;
import org.microblog.dao.impl.BlogDaoImpl;
import org.microblog.dao.impl.CollectionDaoImpl;
import org.microblog.dao.impl.CommentDaoImpl;
import org.microblog.dao.impl.LikesDaoImpl;
import org.microblog.entity.Blog;
import org.microblog.service.BlogService;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Service("BlogService")
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogDao blogDao;
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private CollectionDao collectionDao;
    @Autowired
    private LikesDao likesDao;
    public void setBlogDao(BlogDao blogDao) {
        this.blogDao = blogDao;
    }

    public void setCommentDao(CommentDao commentDao) {
        this.commentDao = commentDao;
    }

    public void setCollectionDao(CollectionDao collectionDao) {
        this.collectionDao = collectionDao;
    }

    public void setLikesDao(LikesDao likesDao) {
        this.likesDao = likesDao;
    }

    @Override
    public Blog getBlogById(int id) throws SQLException {
       // Connection conn = null;
        try{
            //conn  = DatabaseUtil.getConnection();
            return blogDao.findBlogById(id);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
           // DatabaseUtil.closeAll(conn,null,null);
        }
//        Blog blog=blogDao.findBlogById(id);
//        return blog;
    }

    @Override
    public int getForwardCountByBid(int bid) throws SQLException {
        //Connection conn = null;
        try{
            //conn  = DatabaseUtil.getConnection();
            return blogDao.getForwardCountByBid(bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
           // DatabaseUtil.closeAll(conn,null,null);
        }
        //SqlSession sqlSession=sessionFactory.openSession();
        //BlogDao dao=sqlSession.getMapper(BlogDao.class);
//        int ret=blogDao.getForwardCountByBid(bid);
//        //sqlSession.commit();
//        //sqlSession.close();
//        return ret;
    }

    @Override
    public int deleteBlog(int id) throws SQLException {
        //Connection conn = null;
        int result = 0;
        try{
          //  conn = DatabaseUtil.getConnection();
            //conn.setAutoCommit(false);
            //删除该条微博的所有评论
             commentDao.deleteCommentsByBid(id);
            //删除该条微博的所有收藏
             collectionDao.deleteCollectionByBid(id);
            //删除该条微博的所有点赞
             likesDao.deleteLikesByBid(id);
            //删除该条微博
            result = blogDao.deleteBlog(id);
            //conn.commit();
        }catch(SQLException e){
            e.printStackTrace();
//            if(conn!=null){
//                try{
//                    conn.rollback();
//                }catch(SQLException e1){
//                    e.printStackTrace();
//                }
//            }
            throw e;
        }finally{
            //DatabaseUtil.closeAll(conn,null,null);
        }
        return result;
    }

    @Override
    public int getBlogCount(int uId) throws SQLException {
       // SqlSession sqlSession=sessionFactory.openSession();
        //BlogDao dao=sqlSession.getMapper(BlogDao.class);
        int ret=blogDao.getBlogCount(uId);
        //sqlSession.commit();
        //sqlSession.close();
        return ret;
    }

    @Override
    public void getAllBlogsByPage(int uId, String keyword, Page pageObj) throws SQLException {
        //Connection conn = null;
        try{
            //conn = DatabaseUtil.getConnection();
            //BlogDao blogDao = new BlogDaoImpl(conn);
            int totalCount = blogDao.getAllBlogsCount(uId,keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = blogDao.getAllBlogsByPage(uId,keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
           // DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int addNewBlog(Blog blog) throws SQLException {
       // Connection conn = null;
        int result;
        try {
           // conn = DatabaseUtil.getConnection();
           // BlogDao blogDao = new BlogDaoImpl(conn);
            return blogDao.addNewBlog(blog);
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
          // DatabaseUtil.closeAll(conn, null, null);
        }
    }


    @Override
    public void getPopBlogsByPage(int uId, String keyword, Page pageObj) throws SQLException {
        //Connection conn = null;
        try{
           // conn = DatabaseUtil.getConnection();
            //BlogDao blogDao = new BlogDaoImpl(conn);
            int totalCount = blogDao.getPopBlogsCount(uId, keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = blogDao.getPopBlogsByPage(uId, keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
           // DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public void getMyBlogsByPage(int uId, String keyword, Page pageObj) throws SQLException {
       // Connection conn = null;
        try{
           // conn = DatabaseUtil.getConnection();
           // BlogDao blogDao = new BlogDaoImpl(conn);
            int totalCount = blogDao.getBlogCountByUidAndKeyword(uId,keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = blogDao.getMyBlogsByPage(uId,keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
           // DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int forwardBlog(int uId, String content, int forwardBlogId) throws SQLException {
        //Connection conn = null;
        try{
         //   conn = DatabaseUtil.getConnection();
            //BlogDao blogDao = new BlogDaoImpl(conn);
            Blog forwardBlog = blogDao.findBlogById(forwardBlogId);
            int fromBid = 0;
            content = (content != null && !content.equals(""))?content:"转发微博";
            if(forwardBlog.getState() == 2){
                //属于转发的微博
                fromBid = forwardBlog.getOrigin().getId();
            }else{
                //属于原创微博
                fromBid = forwardBlog.getId();
            }
            return blogDao.forwardBlog(uId, content, fromBid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally {
            //DatabaseUtil.closeAll(conn, null, null);
        }
    }
}
